package org.real.guetshop.service.impl;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.context.BaseContext;
import org.real.guetshop.exception.StorageNotEnoughException;
import org.real.guetshop.mapper.ProductMapper;
import org.real.guetshop.mapper.ShopCartMapper;
import org.real.guetshop.pojo.dto.EditProductDTO;
import org.real.guetshop.pojo.dto.ShoppingCartDTO;
import org.real.guetshop.pojo.entity.Product;
import org.real.guetshop.pojo.entity.ShopCart;
import org.real.guetshop.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Resource
    private ShopCartMapper shopCartMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车的商品是否已经存在
        ShopCart shopCart = new ShopCart();
        BeanUtils.copyProperties(shoppingCartDTO, shopCart);
        Integer userId = BaseContext.getCurrentId();
        shopCart.setUserId(userId);

        List<ShopCart> list = shopCartMapper.list(shopCart);

        //如果已经存在了数量+1
        if (list == null || list.size() > 0) {
            ShopCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shopCartMapper.updateNumberById(cart);
        }else {
            //如果不存在添加一条数据
            Integer productId = shoppingCartDTO.getProductId();
            Product product = productMapper.getById(productId);
            shopCart.setName(product.getName());
            shopCart.setImage(product.getImage());
            shopCart.setAmount(product.getPrice());
            shopCart.setUserId(userId);
            shopCart.setNumber(1);
            shopCart.setCreateTime(LocalDateTime.now());
            shopCart.setUnit(product.getUnit());
            shopCartMapper.insert(shopCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShopCart> showShopCart() {
        //获取当前用户id
        Integer userId = BaseContext.getCurrentId();
        ShopCart shopCart = ShopCart.builder()
                .userId(userId)
                .build();

        return shopCartMapper.list(shopCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //获取当前用户id
        Integer userId = BaseContext.getCurrentId();
        shopCartMapper.clean(userId);
    }

    /**
     * 修改商品数量
     * @param editProductDTO
     */
    @Override
    public void editNum(EditProductDTO editProductDTO) throws StorageNotEnoughException {
        editProductDTO.setUserId(BaseContext.getCurrentId());

        shopCartMapper.editById(editProductDTO);

        //判断商品数量是否为0,如果为0删除该条数据
        if (shopCartMapper.getNumber(editProductDTO) == 0){
            shopCartMapper.deleteById(editProductDTO);
        }
    }
}
